package com.xnx.bincker.leader.utils;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 字符串工具
 */
public class StringUtils extends org.springframework.util.StringUtils {
	public static final String NUMBERS = "0123456789";
	public static final String LETTERS = "abcdefghijklmnopqrstuvwxyz";
	public static final String CAPITAL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	/**
	 * 如果字符串一为空则返回字符串二
	 * @param str1 字符串一
	 * @param str2 字符串二
	 */
	public static String emptyOr(@Nullable String str1, String str2) {
		return isEmpty(str1)?str2:str1;
	}

	/**
	 * 获取随机字符串
	 * @param range 范围
	 * @param len 获取的长度
	 */
	public static String getRandomString(CharSequence range, int len){
		StringBuilder stringBuilder = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < len; i++) {
			stringBuilder.append(range.charAt(random.nextInt(range.length())));
		}
		return stringBuilder.toString();
	}

	/**
	 * 获取随机字符串
	 * @param len 长度
	 */
	public static String getRandomString(int len){
		return getRandomString(NUMBERS+LETTERS+CAPITAL, len);
	}

	/**
	 * 获取随机数字字符串
	 * @param len 长度
	 */
	public static String getRandomNumberString(int len){
		return getRandomString(NUMBERS, len);
	}

    /**
     * 首字母小写
     */
	public static String firstCharLowerCase(String str){
        if(!hasText(str)) return "";
        str = str.trim();
        char firstChar = str.charAt(0);
        if(Character.isLowerCase(firstChar)) return str;
        return Character.toLowerCase(firstChar) + str.substring(1);
    }

    /**
     * 首字母大写
     */
	public static String firstCharUpperCase(String str){
	    if(!hasText(str)) return "";
	    str = str.trim();
        Assert.isTrue(hasText(str), "文本不能为空: str=" + str);
        char firstChar = str.charAt(0);
        if(Character.isUpperCase(firstChar)) return str;
        return Character.toUpperCase(firstChar) + str.substring(1);
    }

    /**
     * 转换为驼峰写法
     * @param str 字符串
     * @param separator 分隔符
     */
	public static String toHumpStyle(String str, String separator){
	    if(isEmpty(str)) return "";
        return Arrays.stream(str.split(separator)).map(StringUtils::firstCharUpperCase).collect(Collectors.joining(""));
    }

    /**
     * @see #toHumpStyle(String, String)
     * 默认分隔符 “_”
     */
    public static String toHumpStyle(String str){
	    return toHumpStyle(str, "_");
    }

    /**
     * 把驼峰写法转为链接符链接
     * @param str 驼峰写法文本
     * @param separator 连接符
     */
    public static String separatorHumpStyle(String str, String separator){
        if(isEmpty(str)) return "";
        str = trimAllWhitespace(str);
        List<String> strings = new ArrayList<>();
        char[] chars = str.toCharArray();
        short wordLen = 0;
        char[] word = new char[45];
        for (char c : chars) {
            if(Character.isUpperCase(c)){
                if(wordLen != 0) {
                    strings.add(new String(word, 0, wordLen));
                    wordLen = 0;
                }
                word[wordLen++] = Character.toLowerCase(c);
            }else {
                word[wordLen++] = c;
            }
        }
        if(wordLen > 0) strings.add(new String(word, 0, wordLen));
        return String.join(separator, strings);
    }

    /**
     * @see #separatorHumpStyle(String, String)
     * 默认链接符 “_”
     */
    public static String separatorHumpStyle(String str){
        return separatorHumpStyle(str, "_");
    }

    /**
     * 获取文件路径
     */
    public static String getFilePath(String filePath){
        if(isEmpty(filePath)) return "";
        int index = filePath.lastIndexOf(File.separator);
        if(index == -1) return filePath;
        return filePath.substring(0, index);
    }
}
